<template>
  <div class="edit-dialog">
    <el-dialog title="上传首页图片"
               :visible.sync="dialog"
               :close-on-click-modal="false"
               :show-close="true"
               width="800px"
               center
               @close="handleClose">
      <el-form label-position="right"
               label-width="80px"
               ref="formRef"
               :model="formData"
               :rules="rules"
               class="form">
        <el-form-item>
          <div v-for="(item, index) in imgKeys"
               :key="item.imgkey"
               style="float: left; margin-right: 10px"
               class="img-box">
            <div @click="delImg(index)"
                 class="close-icon">
              <i class="el-icon-error"></i>
            </div>
            <img :src="item.httpUrl"
                 class="uploadImg"
                 style="border: 1px solid #ccc; width: 120px; height: 70px" />
          </div>
          <el-upload class="carousel-upload"
                     drag
                     ref="uploadAdd"
                     action
                     :http-request="uploadImg"
                     :on-success="uploadImgSuccess"
                     :on-remove="handleRemove"
                     multiple
                     show-file-list>
            <!-- <div v-else> -->
            <div class="content">
              <i class="el-icon-upload" />
              <div class="el-upload__text">
                <em>点击上传</em>
              </div>
            </div>

            <!-- </div> -->
            <!--<div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过20MB
            </div>-->
          </el-upload>
        </el-form-item>
      </el-form>
      <el-row style="text-align: center">
        <el-button type="primary"
                   :loading="submitLoading"
                   icon="el-icon-check"
                   @click="submitForm('ruleFormRef')">提 交</el-button>
        <el-button icon="el-icon-close"
                   @click="handleClose">取 消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { setImages, getImagesKeys } from "@/api/carouselManage";
import { upload, getImg } from "@/api/common";
export default {
  name: "carouselManage",
  props: {
    showDialog: false
  },
  data() {
    return {
      titleName: "",
      formData: {},
      editType: 0, // 0 创建项目，1 编辑项目
      imgKeys: [],
      uploadUrls: [],
      projectId: "",
      submitLoading: false,
      rules: {
        name: [{ required: true, message: "请输入项目名称", trigger: "blur" }],
        /*displayName: [
          { required: true, message: "请输入项目别名", trigger: "blur" },
        ],*/
        description: [
          { required: true, message: "请输入项目描述", trigger: "blur" }
        ]
      },
      homePath: "/projectManage"
    };
  },
  computed: {
    dialog: function () {
      return this.showDialog;
    }
  },
  watch: {
    dialog(newVal, oldVal) {
      if (newVal) {
        //this.addFormData = this.formData;
        //this.getMessage()
      }
    }
  },
  created() { },
  mounted() {
    this.getData();
  },
  methods: {
    // 取消事件
    handleClose() {
      this.$emit("carouselDialogFun", false);
    },
    getImgFun(val) {
      let data = {
        key: val
      };
      getImg(data).then(res => {
        for (let i = 0; i < this.imgKeys.length; i++) {
          if (this.imgKeys[i].imgkey == val) {
            this.imgKeys[i].httpUrl = res.value;
          }
        }
      });
    },
    getData() {
      getImagesKeys().then(res => {
        this.formData = res.value;
        for (let i = 0; i < this.formData.images.length; i++) {
          const element = this.formData.images[i];
          this.imgKeys.push({ imgkey: element, httpUrl: "" });

          this.getImgFun(element);
        }
      });
    },
    delImg(index) {
      this.imgKeys.splice(index, 1);
    },
    uploadImg(f) {
      let param = new FormData(); //创建form对象
      param.append("file", f.file); //通过append向form对象添加数据
      param.append("type", "HOME_PIC"); //添加form表单中其他数据
      upload(param) //上传
        .then(response => {
          if (response.code == "200") {
            this.imgKeys.push({
              imgkey: response.value.key,
              httpUrl: response.value.url
            });
            this.$message.success("上传成功!");
            this.close();
          }
        })
        .catch(() => {
          f.onError();
        });
    },
    uploadImgSuccess() {
      // 缓存接口调用所需的文件路径
      console.log("文件上传成功");
      // this.$message.success("上传成功!");
    },
    handleRemove(file, fileList) {
      // 更新缓存文件
      console.log("文件删除");
    },
    back() {
      this.$router.push(this.homePath);
    },
    submitForm(formName) {
      this.submitLoading = true;
      let imageKeys = this.imgKeys.map(item => {
        return item.imgkey;
      });
      this.formData.images = imageKeys;
      this.submitLoading = true;
      setImages(this.formData)
        .then(() => {
          this.submitLoading = false;
          this.$emit("carouselDialogFun", false);
        })
        .catch(() => {
          this.submitLoading = false;
          this.$emit("carouselDialogFun", false);
        });
    }
  }
};
</script>
<style>
.carousel-manage .form {
  max-height: 500px;
  overflow: auto;
}
.carousel-upload .el-upload-dragger {
  border: 1px solid #ccc;
  font-size: 12px;
  width: 120px;
  height: 70px;
}
.carousel-upload .el-upload-dragger .el-icon-upload {
  font-size: 30px;
  margin: 0;
  line-height: 30px;
}
</style>
<style scoped>
.carousel-upload .content {
  display: flex;
  flex-direction: column;
  text-align: center;
  align-items: center;
}
.uploadImg {
  width: auto;
  height: 100%;
}
.img-box {
  position: relative;
}
.img-box .close-icon {
  background-color: #fff;
  width: 25px;
  height: 25px;
  line-height: 25px;
  border-radius: 100%;
  font-size: 25px;
  color: #999;
  position: absolute;
  right: 5px;
  top: 1px;
}
</style>
